//
//  NoticeViewController.m
//  EMMApp
//
//  Created by Mac on 16/5/5.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "NoticeViewController.h"
#import "NoticeCell.h"
#import "RemindDetailsViewController.h"

#import "GetNoticeCategoryListApi.h"
#import "SegmentViewController.h"
#import "ExampleViewController.h"


static CGFloat const ButtonHeight = 50;


@interface NoticeViewController ()

@end

@implementation NoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showHudInWindow:@"正在加载请稍等..."];
    [self postGetProjectListApi];
    [self creatNavigationController];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backPushNotification:) name:@"isBackPushNotification" object:nil];
}

- (void)backPushNotification:(NSNotification *)notification {
    [self dismissViewControllerAnimated:NO completion:^{
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 导航
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationItem.title = @"公告";

    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
    [backBtn addTarget:self
                action:@selector(notBtnClick:)
      forControlEvents:UIControlEventTouchUpInside];
    [backBtn setBackgroundColor:[UIColor clearColor]];
    [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    if (EMM_WIDTH <= 320) {
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }else if (EMM_WIDTH > 375){
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    }else{
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];

    //---------------
}

#pragma mark - 导航返回按钮响应事件
- (void)notBtnClick:(UIButton *)btn {
    if (_isNot == YES) {
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   
}

#pragma mark - 公告Api接口
- (void)postGetProjectListApi {
    GetNoticeCategoryListApi *api = [[GetNoticeCategoryListApi alloc] initWithProjectCode:_projectCode];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetNoticeCategoryListApi *result = (GetNoticeCategoryListApi *)request;
        if (result.isSuccess) {
            GetNoticeCategoryList *get = result.resultData;
            NSMutableArray *arr = [[NSMutableArray alloc] init];
            NSMutableArray *dArr = [[NSMutableArray alloc] init];
            [arr addObject:@"全部"];
            [dArr addObject:@""];
            
            for (NoticeCategoryList *list in get.noticeCategoryList) {
                [arr addObject:list.noticeCategory];
                [dArr addObject:list.noticeCategoryId];
            }
            
            if (dArr.count > 0) {
                SegmentViewController *vc = [[SegmentViewController alloc]init];
                vc.titleArray = arr;
                vc.dataArray = dArr;
                NSMutableArray *controlArray = [[NSMutableArray alloc]init];
                for (int i = 0; i < vc.titleArray.count; i ++) {
                    ExampleViewController *vc = [[ExampleViewController alloc]initWithIndex:i title:arr[i]];
                    [controlArray addObject:vc];
                }
                vc.titleSelectedColor = [UIColor colorFromHexCode:@"#ff7200"];
                vc.subViewControllers = controlArray;
                vc.buttonWidth = EMM_WIDTH/3;
                vc.buttonHeight = ButtonHeight;
                vc.bottomLineColor = [UIColor colorFromHexCode:@"#ff7200"];
                [vc initSegment];
                [vc addParentController:self];
            }else {
                
            }
            
        } else {
            [self showToast:result.resultData.errorMsg];
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

@end
